﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core;
using AG.Editor.TextureFontUI.Windows;

namespace AG.Editor.TextureFontUI.Controls
{
    public partial class AGTextureFontEditPanel : UserControl
    {
        public AGTextureFont _textureFont { get; private set; }
        AGTextureFontCharRectBorder _border;


        public AGTextureFontEditPanel()
        {
            InitializeComponent();

            _ctlEditLeft.TextChanged += new EventHandler(_ctlEditLeft_TextChanged);
            _ctlEditTop.TextChanged += new EventHandler(_ctlEditLeft_TextChanged);
            _ctlEditRight.TextChanged += new EventHandler(_ctlEditLeft_TextChanged);
            _ctlEditBottom.TextChanged += new EventHandler(_ctlEditLeft_TextChanged);

            _border = new AGTextureFontCharRectBorder();
            _border.Width = 0;
            _border.Height = 0;
            _border.MouseMove += new MouseEventHandler(Panel2_MouseMove);
        }

        public void SetTextureFont(AGTextureFont textureFont)
        {
            _textureFont = textureFont;

            string fontsFolder = AGEContext.Current.EProject.GetFolderFonts();
            string textureFullName = string.Format("{0}\\{1}", fontsFolder, _textureFont.TextureFile);

            this.splitContainer1.Panel2.AutoScroll = true;

            _border._texture = new Bitmap(textureFullName);
            _border.Width = _border._texture.Width;
            _border.Height = _border._texture.Height;
            this.splitContainer1.Panel2.Controls.Add(_border);
            RefeshMark();
            BindCharList();
        }


        void Panel2_MouseMove(object sender, MouseEventArgs e)
        {
            _ctlTextPos.Text = string.Format("{0},{1}", e.X, e.Y);
        }

        private void _ctlBtnAddChar_Click(object sender, EventArgs e)
        {
            AGCreateFontCharWindow window = new AGCreateFontCharWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                _textureFont.CharList.Add(window.FontChar);
                _textureFont.HasChanged = true;
                BindCharList();
            }
        }

        private void _ctlBtnRemoveChar_Click(object sender, EventArgs e)
        {
            AGTextureFontChar fontChar = treeView1.SelectedNode.Tag as AGTextureFontChar;
            if (fontChar != null)
            {
                if (MessageBox.Show(string.Format("是否要删除字符[{0}]", fontChar.CharString),
                    "提示",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int index = 0; index < _textureFont.CharList.Count; index++)
                    {
                        if (fontChar.CharString == _textureFont.CharList[index].CharString)
                        {
                            _textureFont.CharList.RemoveAt(index);
                            _textureFont.HasChanged = true;
                            break;
                        }
                    }
                }
            }
        }

        private void BindCharList()
        {
            treeView1.Nodes.Clear();
            for (int index = 0; index < _textureFont.CharList.Count; index++)
            {
                AGTextureFontChar fontChar = _textureFont.CharList[index];
                TreeNode node = new TreeNode();
                node.Text = fontChar.CharString;
                node.Tag = fontChar;
                treeView1.Nodes.Add(node);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            AGTextureFontChar fontChar = e.Node.Tag as AGTextureFontChar;
            _ctlEditLeft.Text = fontChar.Left.ToString();
            _ctlEditTop.Text = fontChar.Top.ToString();
            _ctlEditRight.Text = (fontChar.Left + fontChar.Width).ToString();
            _ctlEditBottom.Text = (fontChar.Top + fontChar.Height).ToString();

            _border._fontChar = fontChar;


            RefeshMark();
        }

        void _ctlEditLeft_TextChanged(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }
            AGTextureFontChar fontChar = treeView1.SelectedNode.Tag as AGTextureFontChar;
            if (fontChar != null)
            {
                int left = fontChar.Left;
                int top = fontChar.Top;
                int right = fontChar.Left + fontChar.Width;
                int bottom = fontChar.Top + fontChar.Height;

                if (int.TryParse(_ctlEditLeft.Text, out left)
                    && int.TryParse(_ctlEditTop.Text, out top)
                    && int.TryParse(_ctlEditRight.Text, out right)
                    && int.TryParse(_ctlEditBottom.Text, out bottom))
                {
                    fontChar.Left = left;
                    fontChar.Top = top;
                    fontChar.Width = right - left;
                    fontChar.Height = bottom - top;
                    RefeshMark();
                    _textureFont.HasChanged = true;
                }
            }
        }

        private void RefeshMark()
        {
            _border.Invalidate();
        }

        private void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            AGTools.Current.TextureFontStore.SaveTextureFont(AGEContext.Current.EProject, _textureFont);
            _textureFont.HasChanged = false;
            MessageBox.Show("保存成功");
        }
    }
}
